import 'package:flutter/material.dart';

class PurchaseOptionsDialog extends StatelessWidget {
  final String chapterTitle;
  final int chapterPrice;
  final int comicPrice;
  final String comicTitle;
  final VoidCallback? onPurchaseChapter;
  final VoidCallback? onPurchaseComic;
  final VoidCallback? onSubscribePremium;

  const PurchaseOptionsDialog({
    super.key,
    required this.chapterTitle,
    required this.chapterPrice,
    required this.comicPrice,
    required this.comicTitle,
    this.onPurchaseChapter,
    this.onPurchaseComic,
    this.onSubscribePremium,
  });

  @override
  Widget build(BuildContext context) {
    return Dialog(
      backgroundColor: Colors.transparent,
      child: Container(
        width: MediaQuery.of(context).size.width * 0.9,
        decoration: BoxDecoration(
          gradient: const LinearGradient(
            begin: Alignment.topLeft,
            end: Alignment.bottomRight,
            colors: [
              Color(0xFF2D1B69),
              Color(0xFF1A0E3D),
            ],
          ),
          borderRadius: BorderRadius.circular(20),
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.3),
              blurRadius: 20,
              offset: const Offset(0, 10),
            ),
          ],
        ),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            // Header
            Container(
              padding: const EdgeInsets.all(20),
              child: Column(
                children: [
                  Icon(
                    Icons.lock_outline,
                    color: Colors.amber,
                    size: 48,
                  ),
                  const SizedBox(height: 12),
                  Text(
                    'Unlock Content',
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 24,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  const SizedBox(height: 8),
                  Text(
                    'Choose your preferred option to continue reading',
                    style: TextStyle(
                      color: Colors.white70,
                      fontSize: 14,
                    ),
                    textAlign: TextAlign.center,
                  ),
                ],
              ),
            ),

            // Options
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 20),
              child: Column(
                children: [
                  // Purchase Chapter Option
                  _buildPurchaseOption(
                    icon: Icons.article_outlined,
                    title: 'Buy Chapter',
                    subtitle: chapterTitle,
                    price: '$chapterPrice Coins',
                    description: 'Unlock this chapter only',
                    onTap: onPurchaseChapter,
                    gradient: const LinearGradient(
                      colors: [Color(0xFF4CAF50), Color(0xFF2E7D32)],
                    ),
                  ),

                  const SizedBox(height: 12),

                  // Purchase Comic Option
                  _buildPurchaseOption(
                    icon: Icons.menu_book_outlined,
                    title: 'Buy Full Comic',
                    subtitle: comicTitle,
                    price: '$comicPrice Coins',
                    description: 'Unlock all chapters of this comic',
                    onTap: onPurchaseComic,
                    gradient: const LinearGradient(
                      colors: [Color(0xFF2196F3), Color(0xFF1565C0)],
                    ),
                    isRecommended: true,
                  ),

                  const SizedBox(height: 12),

                  // Premium Subscription Option
                  _buildPurchaseOption(
                    icon: Icons.workspace_premium,
                    title: 'Go Premium',
                    subtitle: 'Unlimited Access',
                    price: 'Subscribe',
                    description: 'Read all comics without restrictions',
                    onTap: onSubscribePremium,
                    gradient: const LinearGradient(
                      colors: [Color(0xFFFF9800), Color(0xFFE65100)],
                    ),
                    isPremium: true,
                  ),
                ],
              ),
            ),

            // Close Button
            Padding(
              padding: const EdgeInsets.all(20),
              child: TextButton(
                onPressed: () => Navigator.of(context).pop(),
                child: Text(
                  'Cancel',
                  style: TextStyle(
                    color: Colors.white60,
                    fontSize: 16,
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildPurchaseOption({
    required IconData icon,
    required String title,
    required String subtitle,
    required String price,
    required String description,
    required VoidCallback? onTap,
    required Gradient gradient,
    bool isRecommended = false,
    bool isPremium = false,
  }) {
    return Stack(
      children: [
        GestureDetector(
          onTap: onTap,
          child: Container(
            width: double.infinity,
            padding: const EdgeInsets.all(16),
            decoration: BoxDecoration(
              gradient: gradient,
              borderRadius: BorderRadius.circular(12),
              border: isRecommended 
                  ? Border.all(color: Colors.amber, width: 2)
                  : null,
              boxShadow: [
                BoxShadow(
                  color: gradient.colors.first.withOpacity(0.3),
                  blurRadius: 8,
                  offset: const Offset(0, 4),
                ),
              ],
            ),
            child: Row(
              children: [
                // Icon
                Container(
                  padding: const EdgeInsets.all(12),
                  decoration: BoxDecoration(
                    color: Colors.white.withOpacity(0.2),
                    borderRadius: BorderRadius.circular(8),
                  ),
                  child: Icon(
                    icon,
                    color: Colors.white,
                    size: 24,
                  ),
                ),
                
                const SizedBox(width: 16),
                
                // Content
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(
                            title,
                            style: const TextStyle(
                              color: Colors.white,
                              fontSize: 16,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                          if (isPremium)
                            Container(
                              padding: const EdgeInsets.symmetric(
                                horizontal: 8,
                                vertical: 2,
                              ),
                              decoration: BoxDecoration(
                                color: Colors.amber,
                                borderRadius: BorderRadius.circular(10),
                              ),
                              child: const Text(
                                'BEST',
                                style: TextStyle(
                                  color: Colors.black,
                                  fontSize: 10,
                                  fontWeight: FontWeight.bold,
                                ),
                              ),
                            ),
                        ],
                      ),
                      const SizedBox(height: 4),
                      Text(
                        subtitle,
                        style: TextStyle(
                          color: Colors.white.withOpacity(0.9),
                          fontSize: 14,
                        ),
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                      ),
                      const SizedBox(height: 8),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Expanded(
                            child: Text(
                              description,
                              style: TextStyle(
                                color: Colors.white.withOpacity(0.7),
                                fontSize: 12,
                              ),
                            ),
                          ),
                          Container(
                            padding: const EdgeInsets.symmetric(
                              horizontal: 12,
                              vertical: 6,
                            ),
                            decoration: BoxDecoration(
                              color: Colors.white.withOpacity(0.2),
                              borderRadius: BorderRadius.circular(15),
                            ),
                            child: Text(
                              price,
                              style: const TextStyle(
                                color: Colors.white,
                                fontSize: 12,
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
        
        // Recommended Badge
        if (isRecommended)
          Positioned(
            top: -8,
            right: 16,
            child: Container(
              padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
              decoration: BoxDecoration(
                color: Colors.amber,
                borderRadius: BorderRadius.circular(12),
                boxShadow: [
                  BoxShadow(
                    color: Colors.amber.withOpacity(0.3),
                    blurRadius: 4,
                    offset: const Offset(0, 2),
                  ),
                ],
              ),
              child: const Text(
                'RECOMMENDED',
                style: TextStyle(
                  color: Colors.black,
                  fontSize: 10,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
          ),
      ],
    );
  }

  // Static method to show the dialog
  static Future<void> show(
    BuildContext context, {
    required String chapterTitle,
    required int chapterPrice,
    required int comicPrice,
    required String comicTitle,
    VoidCallback? onPurchaseChapter,
    VoidCallback? onPurchaseComic,
    VoidCallback? onSubscribePremium,
  }) {
    return showDialog(
      context: context,
      barrierDismissible: true,
      builder: (context) => PurchaseOptionsDialog(
        chapterTitle: chapterTitle,
        chapterPrice: chapterPrice,
        comicPrice: comicPrice,
        comicTitle: comicTitle,
        onPurchaseChapter: onPurchaseChapter,
        onPurchaseComic: onPurchaseComic,
        onSubscribePremium: onSubscribePremium,
      ),
    );
  }
}